package com.manager.commons.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinyinUtil {

	/**
	 * 首字母
	 * 
	 * @param name
	 * @return
	 */
	public static String getFirstLetter(String name) {
		// 用来存储姓名首字母
		StringBuilder firstLetter = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			char ch = name.charAt(i);
			// 获取汉字的拼音数组
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(ch);
			if (pinyinArray != null && pinyinArray.length > 0) {
				// 取第一个拼音作为首字母
				String pinyin = pinyinArray[0];
				// 统一转换为大写字母
				firstLetter.append(Character.toUpperCase(pinyin.charAt(0)));
			} else {
				// 如果不是汉字，则直接加入姓名首字母中
				firstLetter.append(ch);
			}
		}
		return firstLetter.toString();
	}

	/**
	 * 转拼音
	 * 
	 * @param name
	 * @return
	 */
	public static String toPinyin(String name) {
		if(StringUtil.isEmpty(name))return "";
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		char[] nameCharArray = name.toCharArray();
		StringBuilder pinyin = new StringBuilder();
		String[] pinyinArray = new String[nameCharArray.length];
		for (int i = 0; i < nameCharArray.length; i++) {
			if (Character.toString(nameCharArray[i]).matches("[\u4e00-\u9fa5]")) {
				try {
					pinyinArray[i] = PinyinHelper.toHanyuPinyinStringArray(nameCharArray[i], format)[0];
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinArray[i] = Character.toString(nameCharArray[i]);
			}
			pinyin.append(pinyinArray[i]);
		}
		return pinyin.toString();
	}

	/**
	 * 
	 * @param name
	 * @return
	 */
	public static String[] toPinyins(String name) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		char[] nameCharArray = name.toCharArray();
		String[] pinyinArray = new String[nameCharArray.length];
		StringBuilder pinyin = new StringBuilder();
		StringBuilder zm = new StringBuilder();
		for (int i = 0; i < nameCharArray.length; i++) {
			if (Character.toString(nameCharArray[i]).matches("[\u4e00-\u9fa5]")) {
				try {
					pinyinArray[i] = PinyinHelper.toHanyuPinyinStringArray(nameCharArray[i], format)[0];
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinArray[i] = Character.toString(nameCharArray[i]);
			}
			String py = pinyinArray[i];
			zm.append(py.toUpperCase().charAt(0));
			pinyin.append(py);
		}
		return new String[] { zm.toString(), pinyin.toString() };
	}

	public static void main(String[] args) {
		String chineseName = "大袋鼠";
		String pinyin = toPinyin(chineseName).toUpperCase();
		System.out.println(pinyin);
	}
}
